﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Agnes.Libaray;
using System.IO;
using System.Xml.Linq;
using Caliburn.Micro;

namespace Agnes.TaskManager
{
    /// <summary>
    /// 任务管理器
    /// <remarks>
    /// 用于管理任务概览树和任务的详细信息
    /// </remarks>
    /// </summary>
    public class TaskManager : PropertyChangedBase
    {
        #region Single Instance

        private static TaskManager _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static TaskManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new TaskManager();
                return _instance;
            }
        }

        private TaskManager()
        {
            SlnService.Instance.SlnClosedHandler += new EventHandler(SlnClosed);
            SlnService.Instance.SlnOpenedHanlder += new EventHandler(SlnOpened);
            if (SlnService.Instance.CurrentSln != null)
                SlnOpened(null, null);
        }

        private void SlnOpened(object sender, EventArgs e)
        {
            _taskDir = Path.Combine(SlnService.Instance.CurrentSln.TempDir, "Data_TaskManager");
            _taskXml = Path.Combine(_taskDir, "Tasks.xml");
            TaskRoots = new ObservableCollection<TaskInfo>();
            Load();
        }

        private void SlnClosed(object sender, EventArgs e)
        {
            Save();
            _taskDir = "";
            _taskXml = "";
            TaskRoots = null;
        }
        
        #endregion

        #region Private

        private string _taskDir = "";

        private string _taskXml = "";

        #endregion

        #region Property

        private ObservableCollection<TaskInfo> _taskRoots;
        /// <summary>
        /// 任务树的首层节点
        /// </summary>
        public ObservableCollection<TaskInfo> TaskRoots
        {
            get { return _taskRoots; }
            private set
            {
                _taskRoots = value;
                NotifyOfPropertyChange(() => TaskRoots);
            }
        }

        /// <summary>
        /// 选中的节点
        /// </summary>
        public TaskInfo TaskNodeSelected { get; set; }

        #endregion

        #region Method

        /// <summary>
        /// 从数据库中加载任务
        /// </summary>
        public void Load()
        {
            if (!Directory.Exists(_taskDir))
                Directory.CreateDirectory(_taskDir);
            else
            {
                if (File.Exists(_taskXml))
                {
                    XElement el = XElement.Load(_taskXml);
                    foreach (XElement i in el.Elements("TaskInfo"))
                        TaskRoots.Add(TaskInfo.FetchAsChild(i));
                }
            }
            
        }

        /// <summary>
        /// 将任务信息保存到数据库中
        /// </summary>
        public void Save()
        {
            XElement el = new XElement("TaskManager");

            foreach (TaskInfo i in TaskRoots)
                i.SaveToXml(el.Add);

            el.Save(_taskXml);
        }

        #endregion


    }
}
